package com.HuiShou.controller.user;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.HuiShou.common.utils.LocationUtil;
import com.HuiShou.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.HuiShou.common.annotation.Log;
import com.HuiShou.common.core.controller.BaseController;
import com.HuiShou.common.core.domain.AjaxResult;
import com.HuiShou.common.enums.BusinessType;
import com.HuiShou.domain.UserOrder;
import com.HuiShou.service.IUserOrderService;
import com.HuiShou.common.utils.poi.ExcelUtil;
import com.HuiShou.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author lyz
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/order/userorder")
public class UserOrderController extends BaseController
{
    @Autowired
    private IUserOrderService userOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:userorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserOrder userOrder)
    {
        userOrder.setClientId(SecurityUtils.getUserId());

        // 先查询总数
        List<UserOrder> allList = userOrderService.selectUserOrderList(userOrder);
        int total = allList.size();

        // 再分页查询
        startPage();
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        
        // 计算距离并排序
        list.forEach(e->e.setDistance(LocationUtil.getDistance(userOrder.getUserLongitude(),userOrder.getUserLatitude(),e.getLongitude(),e.getLatitude())));

        // 使用nullsLast处理null值
        list = list.stream()
                .sorted(Comparator.comparing(
                        UserOrder::getDistance,
                        Comparator.nullsLast(Comparator.naturalOrder())
                ))
                .collect(Collectors.toList());

        TableDataInfo rspData = getDataTable(list);
        // 使用正确的总数
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:userorder:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserOrder userOrder)
    {
        List<UserOrder> list = userOrderService.selectUserOrderList(userOrder);
        ExcelUtil<UserOrder> util = new ExcelUtil<UserOrder>(UserOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:userorder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(userOrderService.selectUserOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('order:userorder:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserOrder userOrder)
    {
        return toAjax(userOrderService.insertUserOrder(userOrder));
    }


}
